# 4 - 2 - 2 交叉熵误差函数的实现
import numpy as np

def cross_entropy_error(y, t):
    delta = 1e-7 # 防止log(0)的情况
    return -np.sum(t * np.log(y + delta))

# 测试
t = [0, 0, 1, 0, 0, 0, 0, 0, 0, 0]
y = [0.1, 0.05, 0.6, 0.0, 0.05, 0.1, 0.0, 0.1, 0.0, 0.0]
print(cross_entropy_error(np.array(y), np.array(t))) # 1.0986122886681096

y = [0.1, 0.05, 0.1, 0.0, 0.05, 0.1, 0.0, 0.6, 0.0, 0.0]
print(cross_entropy_error(np.array(y), np.array(t))) # 2.302584092994046